<%@ Page language="vb" CodeFile="recordPayment.aspx.vb" Inherits="recordPayment_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        Over()
        strJS = ""
        strJS = strJS + "[""companyID"", ""<1""],"
        strJS = strJS + "[""paymentDate"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>PAYMENT RECORD<hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='recordPayment.aspx?do=print' method='post' target='_blank' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Company:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='companyID' id='companyID'>")
        Response.Write("<option value=''> Select company  ")
        dbOpen()
        RecSet = Conn.Execute("Select companyID, company From tblCompany")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                companyID = RecSet.Fields("companyID").Value
                company = RecSet.Fields("company").Value
                Response.Write("<option value='" & companyID & "'> " & company & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_companyID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Latest payment date:</td><td align='right'><input type='text' name='paymentDate' id='paymentDate' size='40' maxlength='10' onkeyup='re=/([\d\-]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_paymentDate' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Search'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
    ElseIf Request.QueryString("do") = "print" Then 
        companyID = Request.Form("companyID")
        qPaymentDate = Request.Form("paymentDate")
        companyID = CLng(companyID)
        debitSubtotal = 0
        paymentSubtotal = 0
        roundingSubtotal = 0
        debitGrandtotal = 0
        paymentGrandtotal = 0
        roundingGrandtotal = 0
%>
<script type="text/javascript">
  function printCheck() { 
	if (window.confirm('OBS! Please comfirm that the printouts are OK.')) {
		location.href='recordPayment.aspx?do=setAsPrinted&paymentDate=<%= qPaymentDate %>&companyID=<%= companyID %>';
	}
	else {
		return false;
	}
  }
  </script>
<%
        If IsDate(qPaymentDate) Then 
            dbOpen()
            RecSet = Conn.Execute("Select co.countryID, co.company, co.debitAccount, co.roundingAccount, paymentAccount, c.countryCurrency, c.decimals From (tblCompany co INNER JOIN tblCountry c ON c.countryID = co.countryID) Where co.companyID = " & companyID & "")
            If Not RecSet.EOF Then 
                countryID = RecSet.Fields("countryID").Value
                company = RecSet.Fields("company").Value
                debitAccount = RecSet.Fields("debitAccount").Value
                roundingAccount = RecSet.Fields("roundingAccount").Value
                paymentAccount = RecSet.Fields("paymentAccount").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            OverPrint()
            Response.Write("<div class=noprint>")
            Response.Write("<table>")
            Response.Write("<tr>")
            Response.Write("<td class='headline'>")
            Response.Write("<form>")
            Response.Write("<input type='button' onClick='javascript:print();' value='Print'>&nbsp; ")
            Response.Write("<input type='button' value='Set payment record as printed' onclick=""{return printCheck();} return false;"">")
            Response.Write("</form>")
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("</table>")
            Response.Write("</div>")
            Response.Write("<p style='page-break-after: always'>")
            Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
            Response.Write("<tr>")
            Response.Write("<td colspan='3' class='printBoldHeadline'>Payment record</td>")
            Response.Write("<td colspan='2' align='right' class='printBoldHeadline'>" & company & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td colspan='3' class='printBold'>Record draft up until " & qPaymentDate & "</td>")
            Response.Write("<td colspan='2' align='right' class='printBold'>Page 1 of 2</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td colspan='5'><hr></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td class='printBold'>Payment date</td>")
            Response.Write("<td class='printBold'>Invoice ID</td>")
            Response.Write("<td class='printBold'>Customer</td>")
            Response.Write("<td class='printBold'>Invoice amount (" & countryCurrency & ")<br>Account</td>")
            Response.Write("<td align='right' class='printBold'>Amount (" & countryCurrency & ")</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td colspan='5'><hr></td></tr>")
            dbOpen()
            RecSet = Conn.Execute("Select i.invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND i.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Credit Invoice ****************
            RecSet = Conn.Execute("Select i.invoiceCreditID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceCredit i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND i.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceCreditID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                    paymentGrandtotal = CDbl(paymentGrandtotal) - CDbl(paymentSubtotal)
                    roundingGrandtotal = CDbl(roundingGrandtotal) - CDbl(roundingSubtotal)
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceCreditID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal) * - 1, 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Invoice due to price modification ****************
            RecSet = Conn.Execute("Select i.invoicePriceListModificationID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoicePriceListModification i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND i.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoicePriceListModificationID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    debitSubtotal = CDbl(debitSubtotal) * - 1
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Penalty Invoice Duration ****************
            RecSet = Conn.Execute("Select i.invoiceDurationPenaltyID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceDurationPenaltyID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Credit Penalty Invoice Duration ****************
            RecSet = Conn.Execute("Select i.invoiceCreditDurationPenaltyID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceCreditDurationPenaltyID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                    paymentGrandtotal = CDbl(paymentGrandtotal) - CDbl(paymentSubtotal)
                    roundingGrandtotal = CDbl(roundingGrandtotal) - CDbl(roundingSubtotal)
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal) * - 1, 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Penalty Invoice Distance ****************
            RecSet = Conn.Execute("Select i.invoiceDistancePenaltyID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceDistancePenaltyID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Credit Penalty Invoice Distance ****************
            RecSet = Conn.Execute("Select i.invoiceCreditDistancePenaltyID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceCreditDistancePenaltyID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = CDbl(debitGrandtotal) - CDbl(debitSubtotal)
                    paymentGrandtotal = CDbl(paymentGrandtotal) - CDbl(paymentSubtotal)
                    roundingGrandtotal = CDbl(roundingGrandtotal) - CDbl(roundingSubtotal)
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal) * - 1, 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Service Account Balance Invoice ****************
            RecSet = Conn.Execute("Select i.invoiceServiceAccountBalanceID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceServiceAccountBalanceID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Credit Service Account Balance Invoice ****************
            RecSet = Conn.Execute("Select i.invoiceCreditServiceAccountBalanceID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = v.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceCreditServiceAccountBalanceID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            Response.Flush()
            '*************** Administration Fee Invoice ****************
            RecSet = Conn.Execute("Select i.invoiceAdminFeeID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceAdminFee i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND i.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceAdminFeeID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Flush()
            '*************** Separate Invoice ****************
            dbOpen()
            RecSet = Conn.Execute("Select i.invoiceSeparateID As invoiceID, i.paymentDate, i.invoiceAmount, i.invoiceClaim, p.partnerID, p.partnerName, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = i.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where i.paymentDate <= '" & qPaymentDate & "' AND i.countryID = " & countryID & " AND i.paymentRecord = 0 Order By i.invoiceSeparateID")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    invoiceID = RecSet.Fields("invoiceID").Value
                    paymentDate = RecSet.Fields("paymentDate").Value
                    invoiceAmount = RecSet.Fields("invoiceAmount").Value
                    invoiceClaim = RecSet.Fields("invoiceClaim").Value
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    salestax = RecSet.Fields("salesTax").Value
                    invoiceAmount = invoiceAmount + (CDbl(invoiceAmount) * CDbl(salestax)) / 100
                    invoiceClaim = invoiceClaim + (CDbl(invoiceClaim) * CDbl(salestax)) / 100
                    paymentAmount = CDbl(invoiceAmount) - CDbl(invoiceClaim)
                    invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                    paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
                    roundingAmount = FormatNumber(CDbl(invoiceClaim), 2)
                    debitSubtotal = invoiceAmount
                    paymentSubtotal = paymentAmount
                    roundingSubtotal = roundingAmount
                    debitGrandtotal = debitGrandtotal + debitSubtotal
                    paymentGrandtotal = paymentGrandtotal + paymentSubtotal
                    roundingGrandtotal = roundingGrandtotal + roundingSubtotal
                    Response.Write("<tr>")
                    Response.Write("<td class='printRegular'>" & paymentDate & "</td>")
                    Response.Write("<td class='printRegular'>" & invoiceID & "</td>")
                    Response.Write("<td class='printRegular'>" & partnerID & " - " & partnerName & "</td>")
                    Response.Write("<td class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr><td><br></td></tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitSubtotal) * - 1, 2) & "</td>")
                    Response.Write("</tr>")
                    Response.Write("<tr>")
                    Response.Write("<td colspan='3'></td>")
                    Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
                    Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentAmount), 2) & "</td>")
                    Response.Write("</tr>")
                    If CInt(roundingSubtotal) > 0 Then 
                        Response.Write("<tr>")
                        Response.Write("<td colspan='3'></td>")
                        Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
                        Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingSubtotal), 2) & "</td>")
                        Response.Write("</tr>")
                    End IF
                    Response.Write("<tr><td><br></td></tr>")
                    debitSubtotal = 0
                    paymentSubtotal = 0
                    roundingSubtotal = 0
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Flush()
            Response.Write("</table>")
            Response.Write("</p>")
            debitGrandtotal = CDbl(debitGrandtotal) * - 1
            Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
            Response.Write("<tr>")
            Response.Write("<td class='printBoldHeadline'>Payment record</td>")
            Response.Write("<td align='right' class='printBoldHeadline'>" & company & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td class='printBold'>Record draft up until " & qPaymentDate & "</td>")
            Response.Write("<td align='right' class='printBold'>Page 2 of 2</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td colspan='2'><hr></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td class='printBold'>Account</td>")
            Response.Write("<td align='right' class='printBold'>Amount (" & countryCurrency & ")</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td colspan='2'><hr></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td class='printRegular'>" & debitAccount & "</td>")
            Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(debitGrandtotal), 2) & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td class='printRegular'>" & paymentAccount & "</td>")
            Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(paymentGrandtotal), 2) & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td class='printRegular'>" & roundingAccount & "</td>")
            Response.Write("<td align='right' class='printRegular'>" & FormatNumber(CDbl(roundingGrandtotal), 2) & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td colspan='2'><hr></td></tr>")
            Response.Write("</table>")
            Under()
        Else
            OverPrint()
            Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
            Response.Write("<tr><td class='printBoldHeadline'><font color='red'>The latest payment date " & qPaymentDate & " is not a valid date.</font></td></tr>")
            Response.Write("</table>")
            Under()
        End IF
    ElseIf Request.QueryString("do") = "setAsPrinted" Then 
        paymentDate = Request.QueryString("paymentDate")
        companyID = Request.QueryString("companyID")
        companyID = CLng(companyID)
        dbOpen()
        RecSet = Conn.Execute("Select countryID From tblCompany Where companyID = " & companyID & "")
        countryID = RecSet.Fields("countryID").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        Conn.Execute("Update tblInvoice Set paymentRecord = 1 Where paymentDate <= '" & paymentDate & "' AND countryID = " & countryID & " AND paymentRecord = 0")
        dbClose()
        dbOpen()
        Conn.Execute("Update tblInvoiceCredit Set paymentRecord = 1 Where paymentDate <= '" & paymentDate & "' AND countryID = " & countryID & " AND paymentRecord = 0")
        dbClose()
        dbOpen()
        Conn.Execute("Update tblInvoicePriceListModification Set paymentRecord = 1 Where paymentDate <= '" & paymentDate & "' AND countryID = " & countryID & " AND paymentRecord = 0")
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceDurationPenaltyID As invoiceID From (tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.paymentDate <= '" & paymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                Conn.Execute("Update tblInvoiceDurationPenalty Set paymentRecord = 1 Where invoiceDurationPenaltyID = " & invoiceID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceCreditDurationPenaltyID As invoiceID From (tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.paymentDate <= '" & paymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                Conn.Execute("Update tblInvoiceCreditDurationPenalty Set paymentRecord = 1 Where invoiceCreditDurationPenaltyID = " & invoiceID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceDistancePenaltyID As invoiceID From (tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.paymentDate <= '" & paymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                Conn.Execute("Update tblInvoiceDistancePenalty Set paymentRecord = 1 Where invoiceDistancePenaltyID = " & invoiceID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceCreditDistancePenaltyID As invoiceID From (tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.paymentDate <= '" & paymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                Conn.Execute("Update tblInvoiceCreditDistancePenalty Set paymentRecord = 1 Where invoiceCreditDistancePenaltyID = " & invoiceID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceServiceAccountBalanceID As invoiceID From (tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.paymentDate <= '" & paymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                Conn.Execute("Update tblInvoiceServiceAccountBalance Set paymentRecord = 1 Where invoiceServiceAccountBalanceID = " & invoiceID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceCreditServiceAccountBalanceID As invoiceID From (tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.paymentDate <= '" & paymentDate & "' AND v.countryID = " & countryID & " AND i.paymentRecord = 0")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                Conn.Execute("Update tblInvoiceCreditServiceAccountBalance Set paymentRecord = 1 Where invoiceCreditServiceAccountBalanceID = " & invoiceID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        Conn.Execute("Update tblInvoiceAdminFee Set paymentRecord = 1  Where paymentDate <= '" & paymentDate & "' AND countryID = " & countryID & " AND paymentRecord = 0")
        dbClose()
        dbOpen()
        Conn.Execute("Update tblInvoiceSeparate Set paymentRecord = 1  Where paymentDate <= '" & paymentDate & "' AND countryID = " & countryID & " AND paymentRecord = 0")
        dbClose()
        dbOpen()
        Conn.Execute("Update tblInvoiceCreditSeparate Set paymentRecord = 1  Where paymentDate <= '" & paymentDate & "' AND countryID = " & countryID & " AND paymentRecord = 0")
        dbClose()
%>
  <script>
    self.close()
  </script>
<%
    End IF
%>
